﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Globalization;

namespace TweetMemeNET
{
    /// <summary>
    /// Provides static methods for helping with XML parsing
    /// </summary>
    public static class ParserHelper
    {
        /// <summary>
        /// Returns a string value from an XML element, or an empty string if no value exists
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string GetElementValueOrEmptyString(XElement element)
        {
            return element != null ? element.Value : string.Empty;
        }

        /// <summary>
        /// Returns an integer value from an XML element, or 0 if no value exists
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static int GetElementIntValueOrZero(XElement element)
        {
            return element != null ? int.Parse(element.Value) : 0;
        }

        /// <summary>
        /// Returns a double value from an XML element, or 0.0 if no value exists
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static double GetElementDoubleValueOrZero(XElement element)
        {
            return element != null ? Convert.ToDouble(element.Value,CultureInfo.InvariantCulture) : 0.0;
        }

        /// <summary>
        /// Returns a DateTime value from an XML element, or DateTime.MinValue if no value exists
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static DateTime GetElementDateTimeValueOrMinValue(XElement element)
        {
            return element != null ? DateTime.Parse(element.Value) : DateTime.MinValue;
        }
    }
}
